package com.ajie.ft_time;


import android.app.AlarmManager;
import android.icu.util.TimeZone;
import android.os.Build;
import android.os.Bundle;
import android.os.LocaleList;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ajie.lib_base.ft_time.TimeZoneUtils;
import com.google.gson.Gson;

import java.util.Date;
import java.util.Locale;

/**
 * Created by zhangyingjie on 2020/9/22
 *
 * @function
 */
public class MainActivity extends AppCompatActivity {

    private EditText etTimeZone;
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etTimeZone = findViewById(R.id.et_settimezone);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
//                    logcat -v time process |grep 1036 //查看进程的log 打印的
//                    09-25 12:29:34.522  8043  8060 I MainActivity: run: Thread:Thread-2 id:293
//                    09-25 12:29:34.522  8043  8060 D MainActivity: csdn method:pid: 8043 Thread Tid: 8060 Thread:name Thread-2

                    Thread thread = Thread.currentThread();
                    android.os.Process.myPid();//获取该进程的ID。
                    android.os.Process.myTid();//获取该线程的ID。 相同
                    android.os.Process.myUid();//获取该进程的用户ID。
                    Log.i(TAG, "run: Thread:"+thread.getName()+" id:"+thread.getId()+"   ");//TODO 不同

                    Log.e(TAG,"csdn method:pid: "+android.os.Process.myPid()+" Thread Tid: "+android.os.Process.myTid()+" Thread:name "+Thread.currentThread().getName());
                    if (Build.VERSION.SDK_INT >= 24) {
                        TimeZone timeZone = TimeZone.getDefault();
                        String displayName = timeZone.getDisplayName();
                        String displayName1 = timeZone.getDisplayName(true, TimeZone.SHORT);
                        Log.i(TAG, "run: displaytime:"+displayName+"  displayName1:"+displayName1+"  currentTime:"+new Date()+"  "+System.currentTimeMillis());
                    }else {
                        Log.i(TAG, "run: sdb < 24 error");
                    }

                    Locale locale = null;
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        locale = LocaleList.getDefault().get(0);
                    } else locale = Locale.getDefault();
                    if (locale == null ){
                        Log.e(TAG, "getInfo: locale: null !!!");
                    }
                    //!!!!TODO log结果： getInfo: locale country:  language:en
                    // getInfo: locale country:CN  language:zh
                    // getInfo: locale country:TW  language:zh
                    String currentLanguage = locale.getLanguage();
                    String currentCountry = locale.getCountry();
                    Log.i(TAG, " locale country:"+locale.getCountry()+"  language:"+ currentLanguage+"  ");

                    try {
                        Thread.sleep(1000 * 4);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    int count =0;
    public void setTimeZone(View view) {
        String trim = etTimeZone.getText().toString().trim();
        if (!TextUtils.isEmpty(trim)){
            TimeZoneUtils.getIntance(this).setTimeZone(trim);
        }else {
            count++;
            if (count % 2 == 0) {
//            TimeZoneUtils.getIntance(this).setTimeZone("Asia/Shanghai");
//            Log.e(TAG, "setTimeZone: Shanghai");
                //测试结果OK UTC-3:00 布宜诺斯艾利斯   America/Argentina/Buenos_Aires
                TimeZoneUtils.getIntance(this).setTimeZone("America/Argentina/Buenos_Aires");
                Log.e(TAG, "setTimeZone:  America/Argentina/Buenos_Aires");
            } else {
                TimeZoneUtils.getIntance(this).setTimeZone("Asia/Tokyo");
                Log.e(TAG, "setTimeZone: Tokyo");
            }
        }


    }

    //crash
    public void setTimeZoneTime(View view) {
        AlarmManager mAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        count++;
        if (count%2==0){
            mAlarmManager.setTimeZone("GMT+08:00");
            Log.e(TAG, "setTimeZoneTime: GMT+08:00");
        }else {
            mAlarmManager.setTimeZone("GMT+07:00");
            Log.e(TAG, "setTimeZoneTime: GMT+07:00");
        }

    }


}
